﻿#include "appbase.h"
#include <chmslog.h>

#include <QCoreApplication>
#include <QTextCodec>
#include <QSettings>
#include <QDir>
#include <QFile>


/*
 * 定义，上一次关闭程序是左侧列表的位置
 * */
int AppBase::varItemSelect = 0;

/*
 * 是否开启调试日志,0:不开启，1:开启
 * */
int AppBase::debugRun = 0;

/*
 * 对象计数器
 * */
int AppBase::sNewCounter = 0;

/*
 * 用于中文字符处理
 * @funQStrTextCodec（QByteArray &inText）const， 中文字符转 QString 函数
 * 参数说明: inText 需要转换的中文字符。
 * 使用时直接复制下面代码:
 * ChMSTextCodec ChMSTc;
 * */
QString ChMSTextCodec::funQStrTextCodec(QByteArray &inText) const
{
    QTextCodec* pQTextCodec = QTextCodec::codecForName("GB18030");
    QString ChineseStr = pQTextCodec->toUnicode(inText);
    return ChineseStr;
}

AppBase::AppBase()
{
    // 用于程序名称,版本的显示
    pChMSV = new ChMSVersion;
}

AppBase::~AppBase()
{

}

// 配置文件初始化
void AppBase::funVoidConfigInitializes()
{
    // 程序名称
    ChMSTextCodec ChMSTc;
    QString varAppName = ChMSTc.funQStrTextCodec(pChMSV->ProName);

    // 实例化程序路径
    ChMSAppPath StructAppPath;
    QFile objQFile(StructAppPath.configPath);

    if(!objQFile.exists()){
        QSettings configSeting(StructAppPath.configPath,QSettings::IniFormat);
        configSeting.setIniCodec("UTF-8");

        configSeting.beginGroup("Application");
        configSeting.setValue("name",varAppName);
        configSeting.setValue("version",QString(pChMSV->Version));
        configSeting.endGroup();

        configSeting.beginGroup("LeftMenu");
        configSeting.setValue("leftList","0");
        configSeting.endGroup();

        configSeting.beginGroup("Passwd");
        configSeting.setValue("tabOfPass","0");
        configSeting.endGroup();

        configSeting.beginGroup("IPVersion");
        configSeting.setValue("tabOfIPV","0");
        configSeting.endGroup();

        configSeting.beginGroup("HostDiscover");
        configSeting.setValue("tabOfDiscover","0");
        configSeting.endGroup();

        configSeting.beginGroup("LogDebug");
        configSeting.setValue("debugRun","0");
        configSeting.endGroup();
    }
    else
    {
        QSettings configSeting(StructAppPath.configPath,QSettings::IniFormat);
        configSeting.setIniCodec("UTF-8");

        configSeting.beginGroup("Application");
        configSeting.setValue("name",varAppName);
        configSeting.setValue("version",QString(pChMSV->Version));
        configSeting.endGroup();
    }
}

// 保存配置文件
void AppBase::funVoidWriteConfig(const QString &groupName,const QString &setKey,const QString &setValue) const
{
    ChMSAppPath StructAppPath;

    QSettings configSeting(StructAppPath.configPath,QSettings::IniFormat);
    configSeting.setIniCodec("UTF-8");

    configSeting.beginGroup(groupName);
    configSeting.setValue(setKey,setValue);
    configSeting.endGroup();
}

// 读取配置文件
QString AppBase::funQStrReadConfig(const QString &groupName, const QString &setKey) const
{
    // 返回值
    QString settingValue = "";

    ChMSAppPath StructAppPath;
    QSettings configSeting(StructAppPath.configPath,QSettings::IniFormat);
    configSeting.setIniCodec("UTF-8");

    configSeting.beginGroup(groupName);
    settingValue = configSeting.value(setKey).toString();
    configSeting.endGroup();

    return settingValue;
}

// 写 DNS 配置文件
void AppBase::funVoidWriteDnsIni()
{

}

// 读 DNS 配置文件
QStringList AppBase::funQStrListReadDnsIni()
{
    QStringList abc={};

    return  abc;
}


/*
 * 对象计数器 取数
 * */
int AppBase::sFunGetCounter()
{
    return sNewCounter;
}

/*
 * 重载 new
 * */
void *AppBase::operator new(size_t s)
{
    ++sNewCounter;
    // 程序刚启动时加载配置文件之前是不显示的，因为debug开关标识还未被读取
    std::string str1 = "new of AppBase : ";
    ChMSLog::funVoidViewCounter(str1,sNewCounter);
    return ::operator new(s);
}

/*
 * 重载 new
 * */
void *AppBase::operator new(size_t s,void * p)
{
    ++sNewCounter;
    // 程序刚启动时加载配置文件之前是不显示的，因为debug开关标识还未被读取
    std::string str1 = "new of AppBase : ";
    ChMSLog::funVoidViewCounter(str1,sNewCounter);
    return ::operator new(s,p);
}

/*
 * 重载 delete
 * */
void AppBase::operator delete(void *ptr)
{
    --sNewCounter;
    std::string str1 = "delete of AppBase : ";
    ChMSLog::funVoidViewCounter(str1,sNewCounter);
    return ::operator delete(ptr);
}
